package com.rocker.graduation.util;

import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class FileUtils {
	public static byte[] file2ByteArray(String fileName){
		byte[] data = null;
		File file = new File(fileName);
		FileInputStream inputStream = null;
		ByteArrayOutputStream outputStream = null;
		try {
			inputStream = new FileInputStream(file);
			outputStream = new ByteArrayOutputStream();
			byte[] buf = new byte[inputStream.available()];
			int numBytesRead = 0;
			while ((numBytesRead = inputStream.read(buf)) != -1) {
				outputStream.write(buf, 0, numBytesRead);
			}
			data = outputStream.toByteArray();
			outputStream.flush();
			outputStream.close();
			inputStream.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return data;
	}
	
	public static BufferedImage markImageLogo(byte[] data) throws IOException{
		//Image image = Toolkit.getDefaultToolkit().createImage(data, 0, data.length);
		BufferedImage image = ImageIO.read(new ByteArrayInputStream(data));
		//BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = image.createGraphics();
		graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		graphics.drawImage(image.getScaledInstance(image.getWidth(null), image.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);
		graphics.rotate(Math.toRadians(-45),(double) image.getWidth() / 2,(double) image.getHeight() / 2);
		ImageIcon imgIcon = new ImageIcon("C:\\Rocker\\Workspaces\\Graduation\\RemoteSensing\\src\\main\\webapp\\img\\i_buyer_protection.gif");
		Image icon = imgIcon.getImage();
		float alpha = 0.5f;
		graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
		graphics.drawImage(icon, 150, 150, null);
		graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
		graphics.dispose();
		return image;
	}
}
